package cn.com.checne.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.checne.model.domain.FriendLinkInfo;
import cn.com.checne.model.domain.LanguageInfo;


@Repository
public class FriendLinkInfoDao {
	protected static final Logger log = Logger.getLogger(FriendLinkInfoDao.class);

	/**
	 * create
	 * @param link
	 * @return
	 */
	public boolean create(FriendLinkInfo link) {
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String createSql = "insert into tbl_friendlink(lg_id_fk,link_cate_id,link_url,link_name) values(?,?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(createSql);
			int i = 0 ;
			pstmt.setInt(++i,link.getLanguageId());
			pstmt.setInt(++i,link.getCateId());
			pstmt.setString(++i,link.getUrl());
			pstmt.setString(++i,link.getName());
			pstmt.execute();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result;
	}

	/**
	 * @param link
	 * @return
	 */
	public boolean update(FriendLinkInfo link) {
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String updateSql = "update tbl_friendlink set lg_id_fk = ?,link_cate_id = ? ,link_url = ? ,link_name = ?  where link_id = ? ";
			PreparedStatement pstmt = conn.prepareStatement(updateSql);
			int i = 0 ;
			pstmt.setInt(++i,link.getLanguageId());
			pstmt.setInt(++i,link.getCateId());
			pstmt.setString(++i,link.getUrl());
			pstmt.setString(++i,link.getName());
			pstmt.setInt(++i,link.getLinkId());
			pstmt.executeUpdate();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result;
	}

	/**
	 * @param id
	 * @return
	 */
	public boolean delete(int id){
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String updateSql = "delete from tbl_friendlink where link_id = ? ";
			PreparedStatement pstmt = conn.prepareStatement(updateSql);
			int i = 0 ;
			pstmt.setInt(++i,id);
			pstmt.executeUpdate();
			log.info("[SQL]" + pstmt.toString());
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result ;
	}
	
	/**
	 * 
	 * @param linkId
	 * @return
	 */
	public FriendLinkInfo getFriendLinkInfo(int linkId) {
		FriendLinkInfo link = new FriendLinkInfo();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_friendlink where link_id = ? ";

			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, linkId);
			log.info("[SQL]" + pstmt.toString());
			ResultSet rs = pstmt.executeQuery();
			List<FriendLinkInfo> list = getResult(rs);
			link = (list != null && list.size() > 0 ) ? list.get(0) : link ;
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return link;
	}
	/**
	 * 
	 * @return
	 */
	public List<FriendLinkInfo> getAll(int languageId,int cateId) {
		List<FriendLinkInfo> list = new ArrayList<FriendLinkInfo>();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_friendlink where 1=1" ;
			if(languageId > 0){
				sql += " and lg_id_fk = ? " ;
			}
			if(cateId > 0){
				sql += " and link_cate_id = ? " ;
			}
			PreparedStatement pstmt = conn.prepareStatement(sql);
			int i = 0 ;
			if(languageId > 0){
				pstmt.setInt(++i, languageId);
			}
			if(cateId > 0){
				pstmt.setInt(++i, cateId);
			}
			ResultSet rs = pstmt.executeQuery();
			list.addAll(getResult(rs));
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * get rseult from resultSet
	 * @param rs
	 * @return
	 */
	public List<FriendLinkInfo> getResult(ResultSet rs) {
		List<FriendLinkInfo> list = new ArrayList<FriendLinkInfo>();
		try {
			FriendLinkInfo link = null ;
			while(rs.next()) {
				link = new FriendLinkInfo();
				link.setLinkId(rs.getInt("link_id"));
				link.setCateId(rs.getInt("link_cate_id"));
				link.setName(rs.getString("link_name"));
				link.setUrl(rs.getString("link_url"));
				link.setLanguageId(rs.getInt("lg_id_fk"));
				LanguageInfo lg = new LanguageInfoDao().getLanguageInfoById(link.getLanguageId());
				link.setLanguageInfo(lg);
				list.add(link);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}